#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: title01_2最长的顺子.py 
@version:
@time: 2023/05/27

https://dream.blog.csdn.net/article/details/128985996


3-3-3-3-4-4-5-5-6-7-8-9-10-J-Q-K-A-A-A-A
4-5-6-7-8-8-8

输出
9-10-J-Q-K-A

"""
graph = ["3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
cards = {}
for card in graph:
    cards[card] = 4


def solveMethod(my, over):
    diff(cards, my)
    diff(cards, over)
    print(cards)
    res = find(cards)
    print(res)


def diff(cards, str_):
    for card in str_.split("-"):
        if card in cards:
            cards[card] -= 1


def find(cards):
    res = "NO-CHAIN"
    l, r = 0, 0
    for i in range(0, len(graph)):
        card = graph[i]
        if cards[card] > 0:
            l = i
            while i < len(graph) - 1 and cards[graph[i + 1]] > 0:
                i += 1
            r = i + 1
            if r - l >= 5:
                res = "-".join(graph[l:r])
    return res


my = input()
over = input()
solveMethod(my, over)
